﻿using STMS.Models.DModels;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using Common;
using System.Runtime.Remoting.Messaging;
using STMS.DbUtility;
using System.Runtime.Serialization;

namespace STMS.DAL
{
    public class StoreDAL : BaseDAL<StoreInfo>
    {
        private readonly string connectionString = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
        /// <summary>
        /// 条件查询
        /// </summary>
        /// <param name="keywords">查询关键字</param>
        /// <param name="isDeleted"></param>
        /// <returns></returns>
        public List<StoreInfo> GetStoreInfos(string keywords, int isDeleted)
        {
            string cols = CreateSql.GetColsString<StoreInfo>("isDeleted");

            string query = $"select * from dbo.storeinfos where  (StoreNo like '%{keywords}%' or StoreName like '%{keywords}%') and isDeleted={isDeleted};";


            SqlDataReader reader = SqlHelper.ExecuteReader(query, 1);

            List<StoreInfo> storeList = DbConvert.SqlDataReaderToList<StoreInfo>(reader, "StoreId,StoreNo,StoreName,StoreRegionCount,remark,isDeleted");

            return storeList;
        }

        /// <summary>
        /// 增加仓库DAL层
        /// </summary>
        /// <param name="仓库名字"></param>
        /// <param name="仓库编号"></param>
        /// <param name="仓库描述"></param>
        /// <returns></returns>
        public int AddStore(String storeName, int storeNo, String storeMark)
        {
            int res = 0;
            string query = $"insert into dbo.StoreInfos (StoreName,StoreNo,Remark) VALUES  ('{storeName}',{storeNo},'{storeMark}');";
     
            res = SqlHelper.ExecuteNonQuery(query, 1);
            if (res > 0)
            {
                return 1;
            }
            else
            {
                return 0;
            }

        }
        /// <summary>
        /// 获取指定仓库的分区数
        /// </summary>
        /// <param name="storeId"></param>
        /// <returns></returns>
        public int GetStoreRegionCount(int storeId)
        {
            string query = $"select RegionCount from StoreInfos where StoreId = {storeId}";
            object oCount = SqlHelper.ExecuteScalar(query,1);
            if((oCount != null && oCount.ToString() != ""))
            {
                return oCount.GetInt();
            }
            // -1代表未查询到数据
            return -1;
        }

  
    }
}
 